package com.tsystems.clientEmployee.gui;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.toedter.calendar.JSpinnerDateEditor;
import com.tsysstems.clientEmployee.ClientEmployee;
import com.tsystems.entities.Station;
import com.tsystems.entities.TimeTableElement;
import com.tsystems.entities.Train;

/**
 * @author vlad
 * 
 */
public class AddTTEPanel extends JPanel {

	private static final long serialVersionUID = 5706735981492285638L;
	private JComboBox trainsBox;
	private JComboBox arrivalStationBox;
	private JComboBox departureStationBox;
	private JSpinnerDateEditor arrivalTimeJSDE;
	private JSpinnerDateEditor departureTimeJSDE;

	private TimeTableElement result;
	
	public AddTTEPanel() {
		super(new GridBagLayout());
		setBorder(new EmptyBorder(10, 10, 5, 10));

		add(new JLabel("Поезд"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 10, 10));
		trainsBox = new JComboBox(new Vector<Train>(ClientEmployee
				.getInstance().viewTrains()));
		add(trainsBox, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		add(new JLabel("Станция отправления"), new GridBagConstraints(0, 1, 1,
				1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		departureStationBox = new JComboBox(new Vector<Station>(
				ClientEmployee.getInstance().viewStations()));
		add(departureStationBox, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		add(new JLabel("Время отправления"), new GridBagConstraints(0, 2, 1, 1,
				0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		departureTimeJSDE = new JSpinnerDateEditor();
		add(departureTimeJSDE, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		add(new JLabel("Станция прибытия"), new GridBagConstraints(0, 3, 1, 1,
				0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		arrivalStationBox = new JComboBox(new Vector<Station>(
				ClientEmployee.getInstance().viewStations()));
		add(arrivalStationBox, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		add(new JLabel("Время прибытия"), new GridBagConstraints(0, 4, 1, 1,
				0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		arrivalTimeJSDE = new JSpinnerDateEditor();
		add(arrivalTimeJSDE, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		JButton okBtn = new JButton("Добавить");
		JButton cancelBtn = new JButton("Отмена");
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(okBtn);
		buttonPanel.add(cancelBtn);

		add(buttonPanel, new GridBagConstraints(0, 7, 2, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		okBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				TimeTableElement passenger = new TimeTableElement(((Train)trainsBox
						.getItemAt(trainsBox.getSelectedIndex())),
						(Station)departureStationBox.getItemAt(departureStationBox
								.getSelectedIndex()),
						(Station)arrivalStationBox.getItemAt(arrivalStationBox
								.getSelectedIndex()), departureTimeJSDE
								.getDate(), arrivalTimeJSDE.getDate());
				
				result = passenger;
				
				  if (validate(result)) {
					
					  AddTTEPanel.this.getTopLevelAncestor().setVisible(false);
				}
				 
			}
		});

		cancelBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				result = null;
				AddTTEPanel.this.getTopLevelAncestor().setVisible(false);
			}
		});
	}

	private boolean validate(TimeTableElement passenger) {

		ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
		Validator validator = vf.getValidator();

		String message = validate(passenger, validator);
		if (message != null) {
			JOptionPane.showMessageDialog(AddTTEPanel.this, message);
			return false;
		}

		return true;
	}

	public String validate(Object object, Validator validator) {
		Set<ConstraintViolation<Object>> constraintViolations = validator
				.validate(object);
		String resultString = null;

		if (constraintViolations.size() > 0) {
			resultString = "";

			for (ConstraintViolation<Object> cv : constraintViolations) {
				resultString += cv.getMessage() +"\n";
			}
		}
		return resultString;
	}

	private TimeTableElement getResult() {
		return result;
	}

	public static TimeTableElement showDialog(Window owner) {
		JDialog dialog = new JDialog(owner, "Добавление элемента расписания");
		dialog.setModal(true);
		AddTTEPanel panel = new AddTTEPanel();

		dialog.setContentPane(panel);
		dialog.pack();
		dialog.setLocationRelativeTo(null);
		dialog.setVisible(true);

		return panel.getResult();
	}

}
